package com.wavejo.reactor.service.impl;

import com.wavejo.reactor.pojo.User;
import com.wavejo.reactor.service.UserService;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author WaveHi
 * @Date 26/10/2021 下午 5:09
 * @Version 1.0
 * @Contact HJBWaveHi@163.com
 * @Description
 */
@Service
public class UserServiceImpl implements UserService {

    private Map<Long, User> users = new HashMap<>(16);

    {
        users.put(1L, new User("Tom"));
        users.put(2L, new User("Marry"));
    }

    @Override
    public Mono<User> getUser(Long id) {
        return Mono.justOrEmpty(users.get(id));
    }

    @Override
    public Flux<User> getAllUser() {
        return Flux.fromIterable(users.values());
    }

    @Override
    public Mono<Void> addUser(Mono<User> userMono) {
        return userMono.doOnNext(person -> {
            users.put(Long.valueOf(users.size() + 1), person);
        }).thenEmpty(Mono.empty());
    }
}
